﻿using DbFactory;
using PlaneClean.CommonHelp;
using PlaneClean.Models;
using PlaneCleanEntity;
using PlaneCleanEntity.ViewEntity.SelectTreeEntity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace PlaneClean.Controllers.ManagerController
{
    public class CleanPlaneController : ManagerController
    {
        /// <summary>
        /// 查询飞机清洗配置
        /// </summary>
        /// <param name="pId">飞机id</param>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<t_clean_plane> List(int pId)
        {
            ListResponse<t_clean_plane> response = new ListResponse<t_clean_plane>();
            response.bodyList = new List<t_clean_plane>();
            response.result = "NG";
            if (pId > 0)
            {
                response.bodyList = dbFactory.Db.Queryable<t_clean_plane>().Where(a => a.p_id == pId).ToList();
            }

            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 修改飞机清洗配置
        /// </summary>
        /// <param name="pId">飞机id</param>
        /// <param name="skIdList">喷头id列表</param>
        /// <returns></returns>
        public BaseResponse Update(int pId, string skIdList)
        {
            BaseResponse response = new BaseResponse();
            response.result = "NG";
            List<int> cpIdList = dbFactory.Db.Queryable<t_clean_plane>()
                .Where(a => a.p_id == pId&&a.c_state==1).Select(a => a.sk_id ?? 0).ToList();
            var newSkIdList = skIdList.Split(',')
                .Select<string, int>(x => Convert.ToInt32(x)).Except(cpIdList).ToList();
            var DeleteSkIdList = cpIdList
                .Except(skIdList.Split(',').Select<string, int>(x => Convert.ToInt32(x))).ToList();
            //t_clean_plane cp = new t_clean_plane();
            foreach (var id in newSkIdList)
            {
                t_clean_plane cp = dbFactory.Db.Queryable<t_clean_plane>().First(a=>a.p_id==pId&&a.sk_id==id);
                if (cp == null)
                {
                    cp= new t_clean_plane();
                    cp.c_name = "";
                    cp.c_state = 1;
                    cp.p_id = pId;
                    cp.sk_id = id;
                    cp.sk_level_space = 0;
                    DbFactory.dbFactory.Db.Insertable(cp).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnIdentity();
                }
                else
                {
                    cp.c_state = 1;
                    dbFactory.Db.Updateable(cp).ExecuteCommand();
                }
            }
            dbFactory.Db.Updateable<t_clean_plane>().SetColumns(a=>a.c_state==0)
                .Where(a => DeleteSkIdList.Contains(a.sk_id ?? 0)&&a.p_id==pId).ExecuteCommand();
            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 更新飞机配置喷头水平位置
        /// </summary>
        /// <param name="pId"></param>
        /// <param name="skId"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public BaseResponse UpdateSkPosition(int pId, int skId, int position)
        {
            BaseResponse response = new BaseResponse();
            response.result = "NG";

            t_clean_plane cp = dbFactory.Db.Queryable<t_clean_plane>().First(a => a.p_id == pId && a.sk_id == skId);
            if (cp == null)
            {
                cp = new t_clean_plane();
                cp.c_name = "";
                cp.c_state = 1;
                cp.p_id = pId;
                cp.sk_id = skId;
                cp.sk_level_space = position;
                DbFactory.dbFactory.Db.Insertable(cp).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnIdentity();
            }
            else
            {
                cp.sk_level_space = cp.sk_level_space +position;
                dbFactory.Db.Updateable(cp).ExecuteCommand();
            }
            response.result = "OK";
            return response;
        }

    }
}